﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace LingDong.NaiveCrawler
{
    public class GoogleCrawl
    {
        public static string GetTextResult(string query)
        {
            string url = String.Format(googleTextPattern, query);
            //url = HtmlParser.HtmlUtility.GetEncodedUrl(url, googleEncoding);

            string html = "";
            using (WebClient client = new WebClient())
            {
                client.Headers[HttpRequestHeader.AcceptLanguage] = "zh-CN";
                client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0; Maxthon; GTB6.4; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; SLCC1; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.5.21022; .NET CLR 3.5.30729; InfoPath.2; .NET CLR 3.0.30729)";
                client.Credentials = CredentialCache.DefaultCredentials;

                byte[] data = client.DownloadData(url);
                html = Encoding.UTF8.GetString(data);
                html = client.DownloadString(url);
                //client.DownloadFile(url, "c:\\a.html");
            }
            Console.WriteLine(html);
            return html;
        }

        public static string GetImageResult(string query)
        {
            string url = String.Format(googleImagePattern, query);
            //url = HtmlParser.HtmlUtility.GetEncodedUrl(url, googleEncoding);

            string html = "";
            using (WebClient client = new WebClient())
            {
                //client.Encoding = Encoding.UTF8;
                html = client.DownloadString(url);
                using (StreamWriter sw = new StreamWriter("c:\\b.html", false, Encoding.UTF8))
                {
                    sw.WriteLine(html);
                }
                client.DownloadFile(url, "c:\\a.html");
            }
            Console.WriteLine(html);
            return html;
        }

        public static int GetTextResultNumber(string query)
        {
            return 0;
        }

        public static int GetImageResultNumber(string query)
        {
            return 0;
        }

        private static Encoding googleEncoding = Encoding.UTF8;
        private static string googleTextPattern = @"http://www.google.com.hk/search?hl=zh-CN&newwindow=1&safe=strict&q={0}aq=f&aqi=&aql=&oq=&gs_rfai=";
        private static string googleImagePattern = @"http://www.google.com.hk/images?q={0}";
        private static string baiduPattern = @"http://www.baidu.com/s?wd={0}";
    }
}
